#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<pthread.h>
#include<unistd.h>
#include"Mutex.hpp"
using namespace std;


int i = 10000;
class Pthread_Date{
public:
    pthread_mutex_t* _lock;
    string _pthreadInfo;
    Pthread_Date(pthread_mutex_t* lock,string info):_lock(lock),_pthreadInfo(info){}
};

void* gettickets(void* args){
    Pthread_Date* pdp = static_cast<Pthread_Date*>(args);
    while(true){
        {
            LockGuard guard(pdp->_lock);
            if(i > 0){
                usleep(1024);
                cout<<pdp->_pthreadInfo<<"正在获取票:"<<i<<endl;
                i--;
            }else{
                break;
            }
        
        }
        //usleep(1024);   
    }
    return nullptr;
}


int main(){
    pthread_mutex_t lock;
    pthread_mutex_init(&lock,nullptr);
#define NUM 10
    vector<pthread_t> arr(NUM);
    for(int i = 0; i < NUM ; i++){
        char buffer[1024];
        snprintf(buffer,sizeof(buffer),"这是线程%d",i+1);
        Pthread_Date* PDpoint = new Pthread_Date(&lock,buffer);
        pthread_create(&arr[i],nullptr,gettickets,(void*) PDpoint);
    }
    for(const auto& x : arr){
        pthread_join(x,nullptr);
    }
    
    pthread_mutex_destroy(&lock);

    return 0;
}